package com.one.controller;


import com.github.pagehelper.PageInfo;
import com.one.constant.RabbitMQConstant;
import com.one.producer.RabbitMQProducer;
import com.one.service.CommentService;
import com.one.service.ProductService;
import com.one.util.PageUtil;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.vo.CommentVo;
import com.one.vo.ProductVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Stream;

/**
 * @ClassName: StreetProductController
 * @Auther: yph
 * @Date: 2022/1/14 11:01
 * @Description: 商品详情入口
 */
@Slf4j
@RestController
@RequestMapping("/product")
public class StreetProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CommentService commentService;


    /**
     * @Author: yph
     * @Date: 2022/1/14 16:19
     * @return:
     * @Description: 根据id 查询商品详情
     */
    @GetMapping("/queryProductById")
    public RequestReturn<ProductVo> queryProductById(@RequestParam(value = "userId", required = false) Integer userId,
                                                     @RequestParam(value = "sessionId", required = false) String sessionId,
                                                     @RequestParam("id") Integer id) {
        return RequestReturnFactory.success(productService.queryProductDetailById(userId, id, sessionId));
    }

    /**
     * @Author: yph
     * @Date: 2022/1/14 18:35
     * @return:
     * @Description: 根据商品和是查询条件--- 查询商品评价
     */
    @GetMapping("/queryComments")
    public RequestReturn<PageUtil<List<CommentVo>>> queryCommentByProductId(@RequestParam("productId") Integer productId,
                                                                            @RequestParam(value = "withPicture", required = false) String withPicture,
                                                                            @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                                            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        PageUtil<List<CommentVo>> commentVos = commentService.queryCommentByProduct(productId, withPicture, pageNum, pageSize);
        if (commentVos != null)
            return RequestReturnFactory.success(commentVos);
        return RequestReturnFactory.fail();
    }

    /**
     * @Author: yph
     * @Date: 2022/1/20 10:06
     * @return:
     * @Description: 根据评论id 查询所有子评论
     */
    @GetMapping("/queryCommentsByParentId")
    public RequestReturn<List<CommentVo>> queryCommentByParentId(@RequestParam("parentId") Integer parentId) {
        List<CommentVo> commentVos = commentService.queryCommentByParentId(parentId);
        if (commentVos != null)
            return RequestReturnFactory.success(commentVos);
        return RequestReturnFactory.success();
    }

}
